package com.yc.ioc.bean3_ioc_factorybean;

import com.yc.ioc.bean3_ioc_factorybean.other.Fruit;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zwei
 * @create 2024-07-28 17:28
 */
@Configuration
public class App_FactoryBean {
    public static void main(String[] args) {
        //被spring托管的类    FruitFactoryBean和产品
        ApplicationContext ac=new AnnotationConfigApplicationContext(App_FactoryBean.class);
        FruitFactoryBean ffd= (FruitFactoryBean) ac.getBean("&ffb");  //要获取工厂bean的话，id名必须是&id名
        System.out.println(ffd+"\t"+ffd.hashCode());
        //如果不加&，获取的是产品
        Fruit f= (Fruit) ac.getBean("ffb");
        System.out.println(f);

        //获取spring容器中的所有托管的bean
        String[] beanName=ac.getBeanDefinitionNames();
        for (String s:beanName){
            System.out.println(s);
        }
    }
    @Bean
    public FruitFactoryBean ffb(){
        return new FruitFactoryBean();
    }
}
